//
//  SubViewController.swift
//  BasicExtensions_Example
//
//  Created by JY_NEW on 2022/4/14.
//  Copyright © 2022 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import BasicExtensions

struct STest {
    var block: () -> Void
}

class SubViewController: UIViewController {
    var value = 5
    let queue = DispatchQueue(label: "TEST")
    var block: STest?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .systemGreen
        if let block = block?.block {
            self.queue.asyncAfter(deadline: .now() + 2, execute: block)
        }
        let button = UIButton(.text("测试"), .event(self, #selector(self.onTest(_:))), .color(.systemBlue))
        
        button.setPlugin(TestButtonBorder(color: .red), for: .normal)
        button.setPlugin(TestButtonBorder(color: .blue), for: .selected)
        button.setPlugin(TestButtonBorder(color: .yellow), for: .highlighted)
        button.setPlugin(TestButtonBorder(color: .green), for: .disabled)
        
        button.setTitle("ABCsdfsdfsdf", for: .selected)
        button.setTitle("EFG", for: .highlighted)
        button.setTitle("HIG", for: .disabled)
        
        self.view.build {
            button.layout(.center, .height(44))
        }
        print("FUCK: \(UIButton.State.normal.rawValue)-\(UIButton.State.selected.rawValue)-\(UIButton.State.highlighted.rawValue)-\(UIButton.State.disabled.rawValue)")
    }
    var sub: SubViewController?
    let disposeBag = DisposeBag()
    @objc func onTest(_ button: UIButton) {
        if button.isSelected {
            button.isEnabled = false
            button.isSelected = false
        } else {
            button.isSelected = true
        }
        URLSession(configuration: .default)
            .rx.response(request: "https://www.baidu.com/")
            .compactMap { String(data: $0.data, encoding: .utf8) }
            .cache(key: "baidu", dueTime: .seconds(2), usage: .notUpdate)
            .subscribe(onNext: { text in
                print("FUCK \(text)")
            }).disposed(by: self.disposeBag)
        return ()
        let c = SubViewController()
        c.value = 6
//        c.block = {
//            print(c.value)
//        }
//        c.delay {
//            print(self.value)
//        }
        let block = STest(block: {
            print(self.value)
        })
        c.block = block
        self.block = block
//        self.sub = c
        self.navigationController?.pushViewController(c, animated: true)
    }
    func delay(block: @escaping () -> Void) {
        self.block = .init(block: block)
//        self.queue.asyncAfter(deadline: .now() + 1, execute: block)
    }
    deinit {
        print("SUB die")
    }
}

class TestButtonBorder: BEViewStyleType {
    
    let layer = CALayer()
    
    init(color: UIColor) {
        layer.borderColor = color.cgColor
        layer.borderWidth = 1
    }
    
    func add(to view: UIView) {
        view.layer.insertSublayer(layer, at: 0)
    }
    func remove(from view: UIView) {
        layer.removeFromSuperlayer()
    }
    func layout(for view: UIView) {
        layer.frame = view.bounds
    }
}
